const util = require('util');
const debug = util.debuglog('oil-game:login:remote');

const utils = require('../../../util/utils');


module.exports = function (app) {
    return new Handler(app);
};
let Handler = function (app) {
    this.app = app;
};
let handler = Handler.prototype;

//增加user属性
handler.set = function (uid, key, value, cb) {
    debug("用户" + uid + "  开始增加user属性  " + key + " = " + JSON.stringify(value));
    let user = this.app.loginService.getUser(uid);
    eval(key + "=" + JSON.stringify(value));
    utils.invokeCallback(cb);
}

//删除user属性
handler.del = function (uid, key, cb) {
    debug("用户" + uid + "  开始删除user属性  " + key);
    let user = this.app.loginService.getUser(uid);
    eval("delete " + key);
    utils.invokeCallback(cb);
}

//查询user属性
handler.get = function (uid, key, cb) {
    debug("用户" + uid + "  开始查询user属性  " + key);
    let user = this.app.loginService.getUser(uid);
    let res = eval(key);
    utils.invokeCallback(cb, null, res);
}

//增加user属性的数组元素
handler.push = function (uid, key, item, cb) {
    debug("用户" + uid + "  增加user属性  " + key + " = " + JSON.stringify(item));
    let user = this.app.loginService.getUser(uid);
    eval(key + ".push(" + JSON.stringify(item) + ")");
    utils.invokeCallback(cb);
}

//自定义修改语句，慎用
handler.com = function (uid, comm, cb) {
    console.log('==========>>>>>>>>>>printStack')
    utils.printStack();

    console.log('comm')
    console.log(comm)

    debug("用户" + uid + "  运行自定义语句  " + comm);
    let user = this.app.loginService.getUser(uid);
    let res = eval(comm);

    if (res) utils.invokeCallback(cb, null, res);
    else utils.invokeCallback(cb);
}

//更宽松的自定义语句
handler.com1 = function (uid, comm, cb) {
    debug("用户" + uid + "  运行自定义语句1  " + comm);
    eval(comm);
}

